﻿using Deployment.Data.Environment.Connection;
using Deployment.Data.UI.Controls.Environment;
using Deployment.Data.UI.Dialogs.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Connection
{
    /// <summary>
    /// Interaction logic for ConnectionEditableList.xaml
    /// </summary>
    public partial class ConnectionEditableList : UserControl
    {
        public readonly static DependencyProperty SelectedConnectionProperty =
            DependencyProperty.Register("SelectedConnection",
                typeof(Data.Environment.Connection.AbstractConnection),
                typeof(ConnectionEditableList), null);
        public readonly static DependencyProperty CurrentEnvironmentProperty =
            DependencyProperty.Register("CurrentEnvironment",
                typeof(Data.Environment.Environment),
                typeof(ConnectionEditableList), null);

        public event DependencyPropertyChangedEventHandler PropertyChanged;
        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public Data.Environment.Connection.AbstractConnection SelectedConnection
        {
            get { return (Data.Environment.Connection.AbstractConnection)GetValue(SelectedConnectionProperty); }
            set { SetValueDp(SelectedConnectionProperty, value); }
        }

        public Data.Environment.Environment CurrentEnvironment
        {
            get { return (Data.Environment.Environment)GetValue(CurrentEnvironmentProperty); }
            set { SetValueDp(CurrentEnvironmentProperty, value); }
        }

        public ConnectionEditableList()
        {
            InitializeComponent();
        }

        private void AddConnection_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            EditConnectionDialog dialog = new EditConnectionDialog(parentControl.EnvironmentList.SelectedEnvironment);
            dialog.Owner = Window.GetWindow(this);
            bool? result = dialog.ShowDialog();
            if (result.HasValue && result == true)
            {
                (parentControl.EnvironmentList.SelectedEnvironment as Data.Environment.Environment).AddConnection(dialog.Connection);
                parentControl.HasUnsavedChanges = true;
            }
        }

        private void RemoveConnection_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            Data.Environment.Environment env = parentControl.EnvironmentList.SelectedEnvironment;
            if (env != null)
            {

                AbstractConnection con = this.list.SelectedItem as AbstractConnection;
                if (con != null)
                {
                    MessageBoxResult result = MessageBox.Show(Window.GetWindow(this),
                        string.Format("Are you sure you want to remove connection '{0}' from the '{1}' Environment?", con.Name, env.Name),
                        "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        env.RemoveConnection(con);
                        parentControl.HasUnsavedChanges = true;
                    }
                }
            }
        }

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var Item = this.list.SelectedItem as AbstractConnection;
            SelectedConnection = Item;
        }

        private void lsv_connections_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            var item = ((FrameworkElement)e.OriginalSource).DataContext as AbstractConnection;
            var clone = item.Clone();
            if (item != null)
            {
                EditConnectionDialog dlg = new EditConnectionDialog(parentControl.EnvironmentList.SelectedEnvironment, item);
                dlg.Owner = Window.GetWindow(this);
                bool? result = dlg.ShowDialog();
                if (!result.HasValue || result == false)
                {
                    //(lsv_Environments.SelectedItem as Data.Environment.Environment).Connections
                    item = clone;
                }
            }
        }
    }
}
